package com.czh.springboot04.MyController;

import com.czh.springboot04.entity.Article;
import com.czh.springboot04.service.ArticleService;
import com.czh.springboot04.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("article")
//@CrossOrigin
public class ArticleController {
    @Autowired
    ArticleService articleService;

    @PostMapping("addArticle")
    public R addArticle(@RequestBody Article article){
        int i = articleService.addArticle(article);
        if(i > 0 ){
            return R.ok(article);
        }else {
            return R.fail("失败");
        }
    }

    @DeleteMapping("del")
    public R deleteArticle(int id){
        int i = articleService.deleteArticle(id);
        if(i > 0 ){
            return R.ok("成功");
        }else {
            return R.fail("失败");
        }
    }

    @DeleteMapping("/{id}")
    public R deleteArticle2(@PathVariable("id") int id){
        int i = articleService.deleteArticle(id);
        if(i > 0 ){
            return R.ok("成功");
        }else {
            return R.fail("失败");
        }
    }

    @DeleteMapping("dels")
    public R deleteArticles( int[] ids){
        int i = articleService.deleteArticles(ids);
        if(i > 0){
            return R.ok("批量成功！");
        }else {
            return R.fail("失败");
        }
    }

    @PutMapping("updateA")
    public R updateA(@RequestBody Article article){
        System.out.println(article);
        int i = articleService.updateA(article);
        if(i > 0){
            return R.ok("修改成功！");
        }else {
            return R.fail("失败");
        }
    }

    @RequestMapping("selectAll")
    public R selectAll(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,@RequestParam(value = "pageSize",required = false,defaultValue = "2")int pageSize, Article article){
        //System.out.println(article);
        PageInfo<Article> articlePageInfo = articleService.selectAllInPage(pageNum, pageSize,article);
//        System.out.println(articlePageInfo);
        return R.ok(articlePageInfo);
    }

    @RequestMapping("findById")
    public R findById(int id){
        Article article = articleService.findById(id);
        return R.ok(article);
    }
    @GetMapping("/{id}")
    public R findById2(@PathVariable("id") int id){
        Article article = articleService.findById(id);
        return R.ok(article);
    }

}
